﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic;
using Meqs.Logic.Interface;
using Meqs.Utilities;


public partial class Credit_Teacher_Reprots_CauseTermEvalStu : ContainerBasePage
{
    private CauseFormula causeFormula;
    private CauseLevelTool causeLevelTool;
    protected int T_YEAR
    {
        get
        {
            int id;
            if (int.TryParse(HttpContext.Current.Request["year"], out id))
                return id;
            return -1;
        }
    }
    protected int T_TERM
    {
        get
        {
            int id;
            if (int.TryParse(HttpContext.Current.Request["term"], out id))
                return id;
            return -1;
        }
    }
    protected int T_CLASSID
    {
        get
        {
            int id;
            if (int.TryParse(HttpContext.Current.Request["cls"], out id))
                return id;
            return -1;
        }
    }
    protected int T_STUDENTID
    {
        get
        {
            int id;
            if (int.TryParse(HttpContext.Current.Request["stu"], out id))
                return id;
            return -1;
        }
    }
    protected int T_CAUSEID
    {
        get
        {
            int id;
            if (int.TryParse(HttpContext.Current.Request["couid"], out id))
                return id;
            return -1;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            causeLevelTool = new CauseLevelTool(SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm, SchoolMark);
            btnQuery_Click(null,null);
        }
    }

    
    /// <summary>
    /// 查询按钮事件
    /// </summary>
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        int nYear = T_YEAR;
        int nTerm = T_TERM;
        int nClass = T_CLASSID;
        int nSubject = T_CAUSEID;


        IList<int> causeList = new List<int>();
        causeList.Add(nSubject);

        IList<int> classList = new List<int>();
        causeList.Add(nClass);

        // 按月份的评价
        IList<StuCauseEvalState> stateList =
               Container<IStuCauseEvalHistoryLogic>.Instance.GetState(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                                                                   SchoolCommon.Sch_SchoolInfo.SchoolTerm,
                                                                   SchoolMark, classList, nSubject);
        ListSort<StuCauseEvalState> sort = new ListSort<StuCauseEvalState>(stateList, "StartTime", false);
        stateList = sort.Sort();
        IList<DateTime> evalMonthList = new List<DateTime>();
        foreach (StuCauseEvalState state in stateList)
        {
            if (!evalMonthList.Contains(new DateTime(state.StartTime.Year, state.StartTime.Month, 1)))
                evalMonthList.Add(state.StartTime);
        }

        // 获取评价规则大类
        IList<StuCauseEvalType> evalTypeList =
            Container<IStuCauseEvalTypeLogic>.Instance.GetAllStuCauseEvalType(nSubject, SchoolMark, nYear, nTerm);

        // 获取期末成绩
        IList<TestScoreInTermView> testScoreList =
            Container<IClassResultExamListLogic>.Instance.FindTestScoreInTerm(SchoolMark, nYear, nTerm, null, nClass, causeList);

        // 获取学科学分成绩
        IList<CreditReport> causeScoreList =
            Container<IStuCausePersonCatchLogic>.Instance.GetCreditReport(SchoolMark, 0, nClass, nYear, nTerm);


        CreateTable(evalTypeList, testScoreList, causeScoreList, evalMonthList, nSubject);
    }

    /// <summary>
    /// 构造表格
    /// </summary>
    private void CreateTable(IList<StuCauseEvalType> evalTypeList, IList<TestScoreInTermView> testScoreList, IList<CreditReport> causeScoreList, IList<DateTime> evalMonthList, int nSubject)
    {
        causeFormula = new CauseFormula(SchoolMark, nSubject);
        causeLevelTool = new CauseLevelTool(SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm, SchoolMark);
        tbResult.Rows.Clear();

        int width = 0;
        int index = 1;
        #region 构造表头
        TableHeaderRow hrow = new TableHeaderRow();
        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "序号";
        hcell.Style[HtmlTextWriterStyle.Width] = "50px";
        hcell.CssClass = "FixedRowCol";
        width += 50;
        hrow.Cells.Add(hcell);

        hcell = new TableHeaderCell();
        hcell.Text = "学号";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        hcell.CssClass = "FixedRowCol";
        width += 70;
        hrow.Cells.Add(hcell);

        hcell = new TableHeaderCell();
        hcell.Text = "姓名";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        hcell.CssClass = "FixedRowCol";
        width += 70;
        hrow.Cells.Add(hcell);
        int nindex = 2;
        //// 加入月份列
        //if (evalMonthList != null)
        //{
        //    foreach (DateTime time in evalMonthList)
        //    {
        //        hcell = new TableHeaderCell();
        //        hcell.Text = String.Format("{0}月", time.Month);
        //        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        //        width += 70;
        //        hcell.CssClass = "FixedRow";
        //        hrow.Cells.Add(hcell);
        //        nindex++;
        //    }
        //}

        // 加入规则类别列
        if (evalMonthList != null)
        {
            foreach (StuCauseEvalType type in evalTypeList)
            {
                hcell = new TableHeaderCell();
                hcell.Text = type.Caption;
                hcell.Style[HtmlTextWriterStyle.Width] = "70px";
                width += 70;
                hcell.CssClass = "FixedRow";
                hrow.Cells.Add(hcell);
                nindex++;
            }
        }

        hcell = new TableHeaderCell();
        hcell.Text = "累计得分";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        width += 70;
        hcell.CssClass = "FixedRow";
        hrow.Cells.Add(hcell);

        hcell = new TableHeaderCell();
        hcell.Text = "平时学分";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        width += 70;
        hcell.CssClass = "FixedRow";
        hrow.Cells.Add(hcell);

        hcell = new TableHeaderCell();
        hcell.Text = "期末成绩";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        width += 70;
        hcell.CssClass = "FixedRow";
        hrow.Cells.Add(hcell);

        hcell = new TableHeaderCell();
        hcell.Text = "成绩学分";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        width += 70;
        hcell.CssClass = "FixedRow";
        hrow.Cells.Add(hcell);

        nindex += 5;
        hcell = new TableHeaderCell();
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        width += 70;
        hcell.CssClass = "FixedRow";
        hcell.Attributes.Add("onclick", String.Format("sort({0})", nindex));
        hcell.Text = "学期得分" + String.Format("<span id='img{0}'><img src='../../Common/images/1.gif' /></span>", nindex);
        hrow.Cells.Add(hcell);

        hcell = new TableHeaderCell();
        hcell.Text = "等级评定";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        width += 70;
        hcell.CssClass = "FixedRow";
        hrow.Cells.Add(hcell);

        tbResult.Rows.Add(hrow);
        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);
        #endregion

        #region 填充表格内容
        int oldStudentID = -1;
        TableRow row = null;
        TableCell cell = null;
        IList<TableCell> monthCells = null;
        IList<TableCell> typeCells = null;
        TableCell sumScore = null;      // 累计得分单元格
        TableCell causeScore = null;    // 平时学分
        TableCell testScore = null;     // 期末成绩
        TableCell testCredit = null;    // 成绩学分
        TableCell termScore = null;     // 学期得分
        TableCell ranking = null;       // 等级评定
        double dbSumScore = 0;
        double dbCauseScore = 0;
        double dbTestCredit = 0;
        Dictionary<String, double> monthScoreList = null;
        Dictionary<String, double> typeScoreList = null;

        foreach (CreditReport report in causeScoreList)
        {

            if (report.StuID != oldStudentID)
            { // 新建一行 
                if (oldStudentID != -1)
                { // 统计上一行数据 
                    //// 填充月份列的分数
                    //foreach (TableCell monthCell in monthCells)
                    //{
                    //    if (monthScoreList.ContainsKey(monthCell.Attributes["month"]))
                    //    {
                    //        monthCell.Text = monthScoreList[monthCell.Attributes["month"]].ToString();
                    //    }
                    //    else
                    //    {
                    //        monthCell.Text = "0";
                    //    }
                    //    row.Cells.Add(monthCell);
                    //}
                    // 填充类别列的分数
                    foreach (TableCell typeCell in typeCells)
                    {
                        if (typeScoreList.ContainsKey(typeCell.Attributes["type"]))
                        {
                            dbSumScore += typeScoreList[typeCell.Attributes["type"]];
                            typeCell.Text = typeScoreList[typeCell.Attributes["type"]].ToString();
                        }
                        else
                            typeCell.Text = "0";
                        row.Cells.Add(typeCell);
                    }
                    // 填充累计得分
                    sumScore.Text = dbSumScore.ToString();
                    row.Cells.Add(sumScore);
                    // 填充平时学分
                    dbCauseScore = causeFormula.GetExpressScore(dbSumScore);
                    causeScore.Text = Math.Round(dbCauseScore, 2).ToString();
                    row.Cells.Add(causeScore);
                    // 期末成绩

                    row.Cells.Add(testScore);
                    // 成绩学分

                    row.Cells.Add(testCredit);
                    // 填充学期学分
                    termScore.Text = Math.Round((dbCauseScore + dbTestCredit), 2).ToString();
                    row.Cells.Add(termScore);
                    // 等级评定
                    ranking.Text = causeLevelTool.GetCauseLevel(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                        SchoolCommon.Sch_SchoolInfo.SchoolTerm,
                        (dbCauseScore + dbTestCredit));
                    row.Cells.Add(ranking);
                    if (oldStudentID == T_STUDENTID)
                    {
                        tbResult.Rows.Add(row);
                    }
                }
                dbSumScore = 0;
                dbCauseScore = 0;
                monthScoreList = new Dictionary<String, double>();
                typeScoreList = new Dictionary<String, double>();
                row = new TableRow();
                // 序号列
                cell = new TableCell();
                cell.Text = index.ToString();
                cell.CssClass = "FixedCol";
                row.Cells.Add(cell);
                // 学号列
                cell = new TableCell();
                cell.Text = report.StuCode.ToString();
                cell.CssClass = "FixedCol";
                row.Cells.Add(cell);
                // 姓名列
                cell = new TableCell();
                cell.Text = report.StuName.ToString();
                cell.CssClass = "FixedCol";
                row.Cells.Add(cell);
                //// 月份列
                //monthCells = new List<TableCell>();
                //foreach (DateTime time in evalMonthList)
                //{
                //    cell = new TableCell();
                //    cell.Attributes.Add("month", time.ToString("yyyy-MM"));
                //    monthCells.Add(cell);
                //}
                // 类别列
                typeCells = new List<TableCell>();
                foreach (StuCauseEvalType type in evalTypeList)
                {
                    cell = new TableCell();
                    cell.Attributes.Add("type", type.RID.ToString());
                    typeCells.Add(cell);
                }
                // 累计得分
                sumScore = new TableCell();
                // 平时学分
                causeScore = new TableCell();
                // 期末成绩
                testScore = new TableCell();
                TestScoreInTermView ts = (testScoreList as List<TestScoreInTermView>).Find(delegate(TestScoreInTermView c) { return c.StuID == report.StuID; });
                testScore.Text = (ts != null && ts.Score.HasValue) ? Math.Round(ts.Score.Value, 2).ToString() : "0";
                // 成绩学分
                testCredit = new TableCell();
                dbTestCredit = causeFormula.GetTestScore((ts != null && ts.Score.HasValue) ? ts.Score.Value : 0);
                testCredit.Text = Math.Round(dbTestCredit, 2).ToString();
                // 学期得分
                termScore = new TableCell();
                // 等级评定
                ranking = new TableCell();
                index++;
            }
            //// 月份计算
            //String strDate = report.StartTime.ToString("yyyy-MM");
            //if (nSubject == report.CauseID)
            //{
            //    if (!monthScoreList.ContainsKey(strDate))
            //        monthScoreList.Add(strDate, report.TypeScore);
            //    else
            //        monthScoreList[strDate] += report.TypeScore;
            //}

            // 类别计算
            String strTypeID = report.TypeID.ToString();
            if (!typeScoreList.ContainsKey(strTypeID))
                typeScoreList.Add(strTypeID, report.TypeScore);
            else
                typeScoreList[strTypeID] += report.TypeScore;

            oldStudentID = report.StuID;
        }
        foreach (TableCell typeCell in typeCells)
        {
            if (typeScoreList.ContainsKey(typeCell.Attributes["type"]))
            {
                dbSumScore += typeScoreList[typeCell.Attributes["type"]];
                typeCell.Text = typeScoreList[typeCell.Attributes["type"]].ToString();
            }
            else
                typeCell.Text = "0";
            row.Cells.Add(typeCell);
        }
        // 填充累计得分
        sumScore.Text = dbSumScore.ToString();
        row.Cells.Add(sumScore);
        // 填充平时学分
        dbCauseScore = causeFormula.GetExpressScore(dbSumScore);
        causeScore.Text = Math.Round(dbCauseScore, 2).ToString();
        row.Cells.Add(causeScore);
        // 期末成绩

        row.Cells.Add(testScore);
        // 成绩学分

        row.Cells.Add(testCredit);
        // 填充学期学分
        termScore.Text = Math.Round((dbCauseScore + dbTestCredit), 2).ToString();
        row.Cells.Add(termScore);
        // 等级评定
        ranking.Text = causeLevelTool.GetCauseLevel(SchoolCommon.Sch_SchoolInfo.SchoolYear,
            SchoolCommon.Sch_SchoolInfo.SchoolTerm,
            (dbCauseScore + dbTestCredit));
        row.Cells.Add(ranking);
        if (oldStudentID == T_STUDENTID)
            tbResult.Rows.Add(row);

        if (oldStudentID != -1)
        { // 统计上一行数据 
            //// 填充月份列的分数
            //foreach (TableCell monthCell in monthCells)
            //{
            //    if (typeScoreList.ContainsKey(monthCell.Attributes["month"]))
            //    {
            //        dbSumScore += monthScoreList[monthCell.Attributes["month"]];
            //        monthCell.Text = monthScoreList[monthCell.Attributes["month"]].ToString();
            //    }
            //}
            // 填充类别列的分数
            foreach (TableCell typeCell in typeCells)
            {
                if (typeScoreList.ContainsKey(typeCell.Attributes["type"]))
                    typeCell.Text = typeScoreList[typeCell.Attributes["type"]].ToString();
            }
            // 填充累计得分
            sumScore.Text = dbSumScore.ToString();
            // 填充平时学分
            dbCauseScore = causeFormula.GetExpressScore(dbSumScore);
            causeScore.Text = Math.Round(dbCauseScore, 2).ToString();
            // 填充学期学分
            termScore.Text = Math.Round((dbCauseScore + dbTestCredit), 2).ToString();
            // 等级评定
            ranking.Text = causeLevelTool.GetCauseLevel(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                SchoolCommon.Sch_SchoolInfo.SchoolTerm,
                (dbCauseScore + dbTestCredit));
        }
        #endregion
    }


}
